package com.youkeyi.ddy.cloud.basic.pub.dictionary.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.pub.dictionary.entity.SysDictionary;
import com.youkeyi.ddy.cloud.basic.pub.dictionary.service.SysDictionaryService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.PublicDictionaryCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.dto.PublicDictionaryByBusinessCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.dto.PublicDictionaryListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.dto.PublicDictionaryListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.param.*;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统字典表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-03-21
 */
@Service
public class SysDictionaryController implements PublicDictionaryCloudService {

    @Autowired
    private SysDictionaryService sysDictionaryService;

    @Override
    public PublicDictionaryListCloudDto query(PublicDictionaryQueryCloudParam param) {
        PublicDictionaryListCloudDto dto = new PublicDictionaryListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getDictionaryPrimaryKeys())) {
            param.getDictionaryPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getDictTypes())) {
            param.getDictTypes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getDictKeys())) {
            param.getDictKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysDictionary> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getDictionaryPrimaryKeys()), SysDictionary::getPrimaryKey, param.getDictionaryPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysDictionary::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getDictTypes()), SysDictionary::getDictType, param.getDictTypes())
                .in(CollectionUtils.isNotEmpty(param.getDictKeys()), SysDictionary::getDictKey, param.getDictKeys())
                .ne(SysDictionary::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysDictionary> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysDictionary> dictionaryList = sysDictionaryService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(dictionaryList.getRecords())) {
            dto.setTotal(dictionaryList.getTotal());

            List<PublicDictionaryListItemCloudDto> items = new ArrayList<>();
            dictionaryList.getRecords().forEach(dictionary -> {
                PublicDictionaryListItemCloudDto item = new PublicDictionaryListItemCloudDto();
                item.setDictionaryPrimaryKey(dictionary.getPrimaryKey());
                item.setCorpKey(dictionary.getCorpKey());
                item.setDictType(dictionary.getDictType());
                item.setDictTypeName(dictionary.getDictTypeName());
                item.setDictKey(dictionary.getDictKey());
                item.setDictKeyName(dictionary.getDictKeyName());
                item.setDictValue(dictionary.getDictValue());
                item.setSortNo(dictionary.getSortNo());
                item.setIconUrl(dictionary.getIconUrl());
                item.setDictDesc(dictionary.getDictDesc());
                item.setIsDeleted(dictionary.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(dictionary.getCreateTime() != null ? dictionary.getCreateTime().getTime() : null);
                item.setCreateUserKey(dictionary.getCreateUserKey());
                item.setCreateUserName(dictionary.getCreateUserName());
                item.setUpdateTime(dictionary.getUpdateTime() != null ? dictionary.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(dictionary.getUpdateUserKey());
                item.setUpdateUserName(dictionary.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public PublicDictionaryByBusinessCloudDto queryByBusiness(PublicDictionaryQueryByBusinessCloudParam param) {
        PublicDictionaryByBusinessCloudDto dto = new PublicDictionaryByBusinessCloudDto();
        if (null == param || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getDictType())) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        QueryWrapper<SysDictionary> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(SysDictionary::getCorpKey, param.getCorpKey())
                .eq(SysDictionary::getDictType, param.getDictType())
                .ne(SysDictionary::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        List<SysDictionary> dictionaryList = sysDictionaryService.list(wrapper);
        if (CollectionUtils.isNotEmpty(dictionaryList)) {
            dto.getDictionary().putAll(dictionaryList.stream().collect(Collectors.toMap(SysDictionary::getDictKey, SysDictionary::getDictValue, (k1, k2) -> k1)));
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(PublicDictionaryCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getDictType()) || StringUtils.isBlank(param.getDictKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysDictionary dictionary = new SysDictionary();
        dictionary.setPrimaryKey(SimpleUUidUtils.getUuid());
        dictionary.setCorpKey(param.getCorpKey());
        dictionary.setDictType(param.getDictType());
        dictionary.setDictTypeName(param.getDictTypeName());
        dictionary.setDictKey(param.getDictKey());
        dictionary.setDictKeyName(param.getDictKeyName());
        dictionary.setDictValue(param.getDictValue());
        dictionary.setSortNo(param.getSortNo());
        dictionary.setIconUrl(param.getIconUrl());
        dictionary.setDictDesc(param.getDictDesc());
        dictionary.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        dictionary.setCreateUserKey(param.getCreateUserKey());
        dictionary.setCreateUserName(param.getCreateUserName());
        dictionary.setCreateTime(new Date());

        boolean save = sysDictionaryService.save(dictionary);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(dictionary.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(PublicDictionaryUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getDictionaryPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysDictionary> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysDictionary::getPrimaryKey, param.getDictionaryPrimaryKey());
        SysDictionary dictionary = new SysDictionary();
        dictionary.setPrimaryKey(param.getDictionaryPrimaryKey());
        dictionary.setDictType(param.getDictType());
        dictionary.setDictTypeName(param.getDictTypeName());
        dictionary.setDictKey(param.getDictKey());
        dictionary.setDictKeyName(param.getDictKeyName());
        dictionary.setDictValue(param.getDictValue());
        dictionary.setSortNo(param.getSortNo());
        dictionary.setIconUrl(param.getIconUrl());
        dictionary.setDictDesc(param.getDictDesc());
        dictionary.setUpdateUserKey(param.getUpdateUserKey());
        dictionary.setUpdateUserName(param.getUpdateUserName());
        dictionary.setUpdateTime(new Date());
        boolean save = sysDictionaryService.update(dictionary, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(PublicDictionaryDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getDictionaryPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysDictionary> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysDictionary::getPrimaryKey, param.getDictionaryPrimaryKeys());

        SysDictionary dictionary = new SysDictionary();
        dictionary.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        dictionary.setUpdateUserKey(param.getUpdateUserKey());
        dictionary.setUpdateUserName(param.getUpdateUserName());
        dictionary.setUpdateTime(new Date());
        boolean save = sysDictionaryService.update(dictionary, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
    
}
